<?php
/**
 * Created by PhpStorm.
 * User: testypp
 * Date: 2018/1/2
 * Time: 下午4:24
 */


class Model_Comment extends PhalApi_Model_NotORM {

    // 根据topicID获取comment
    public function getCommentByTopicID ($topicid) {
        return $this->getORM()
            ->select('*')
            ->where('topicid = ?', $topicid)
            ->fetchAll();
    }



    // 增加评论
    public function addComment ($userid, $username, $topicid, $commentstring, $ishot) {

        // 生成当前时间
        $date = date("Y-m-d H:i:s");

        $encodeComment = $this->userTextEncode($commentstring);

        $data = array('userid' => $userid, 'username' => $username, 'ishot' => $ishot, 'time' => $date, 'commentstring' => $encodeComment, 'topicid' => $topicid);

        return $this->getORM()->insert($data);

    }

    function userTextEncode($str){
        if(!is_string($str))return $str;
        if(!$str || $str=='undefined')return '';

        $text = json_encode($str); //暴露出unicode
        $text = preg_replace_callback("/(\\\u[ed][0-9a-f]{3})/i",function($str){
            return addslashes($str[0]);
        },$text); //将emoji的unicode留下，其他不动，这里的正则比原答案增加了d，因为我发现我很多emoji实际上是\ud开头的，反而暂时没发现有\ue开头。
        return json_decode($text);
    }

}